<?php
/*
 * 2/7/2011
 * /models/youtube_model/youtube_video.php
 */
class M_Movie extends CI_Model{
    private $_table = 'movies';
    var $_movie_id;
    var $_movie_category_id;
    var $_name = '';
    var $_main_content = '';
    var $_introduction = '';
    var $_image = '';
    var $_vote = 0;
    var $_total_votes = 0;
    var $_actors = '';
    var $_year;
    var $_date;
    
    function __construct() {
        parent::__construct();        
    }
        
    /**
     *lay toan bo thong tin cua video
     * @param type $movieID movie id
     * @return type movie array
     */
    function getMovieInfo($movieID){
        $this->db->cache_on();
        $sql = "SELECT c.movie_category_id AS category_id, c.name AS category_name, m.name AS movie_name,
        m.main_content AS movie_main_content,
m.introduction AS movie_introduction, m.image AS movie_image, m.date AS movie_date, sum( ep.views ) AS movie_views, m.rating AS movie_rating, m.total_votes AS movie_total_votes, m.actors AS movie_actors, m.year AS movie_year
                FROM movies m, movie_episode ep, movie_categories c
                WHERE c.movie_category_id = m.movie_category_id
                AND ep.movie_id = m.movie_id
                AND m.movie_id = ?
                GROUP BY c.movie_category_id, c.name, m.name, m.introduction, m.image, m.date, m.rating, m.total_votes, m.actors, m.year
                LIMIT 0 , 1";
        
        $query = $this->db->query($sql, $movieID);
                
        $row = $query->row_array();
        return $row;        
    }

    /** lay danh sach cac episode cua movie nay
     * @param  $movieID
     * @return array of episode
     */
    function getEpisodeList($movieID){
        $this->db->cache_on();
        //query lay ra danh sach cac tap film
        $sql = 'select ep.episode_id, ep.name
              from movie_episode ep, movies mv
              where ep.movie_id = mv.movie_id and mv.movie_id = ' . $movieID .
              ' order by ep.episode_id asc';
        $query = $this->db->query($sql);
        
        //if ket qua la chuoi rong thi load default
        if($query->num_rows() == 0){
            
        }        
        return $query->result_array();
    }
    
    /**
     * lay danh sach video theo yeu cau
     */

    /**
     * @param  $rowPerPage
     * @return int
     */
    function maxPage($rowPerPage){
        $this->db->cache_on();
        $sql = "SELECT count(*) as numrows
                FROM movies m
                ";
        $query = $this->db->query($sql);
        $temp = $query->row_array();
        $numrows = $temp['numrows'];

        return ceil($numrows / $rowPerPage) - 1; //so trang toi da

    }

    /**
     * @param  $offset vi tri bat dau lay record
     * @param  $rowsPerPage record trong 1 trang
     * @return movie array
     */
    function getLatestMovies($offset, $rowsPerPage){
        $this->db->cache_on();
        $offset = $offset > 0 ? $offset : 0;
        $sql = "SELECT m.movie_id, m.name, m.introduction, m.date, m.image, m.rating, m.actors, sum( ep.views ) as views
                FROM movies m, movie_episode ep
                WHERE ep.movie_id = m.movie_id
                GROUP BY m.movie_id, m.name, m.introduction, m.date, m.image, m.rating, m.actors
                ORDER BY m.date DESC
                LIMIT $offset , $rowsPerPage";

        $query = $this->db->query($sql);
        return $query->result_array();        
    }
    
    /**
     * @param  $offset
     * @param  $rowPerPage
     * @return movie array
     */
    function getMostReviewMovies($offset, $rowPerPage){
        $this->db->cache_on();
        $sql = "SELECT m.movie_id, m.name, m.introduction, m.date, m.image, m.rating, m.actors, sum( ep.views ) as views
                FROM movies m, movie_episode ep
                WHERE ep.movie_id = m.movie_id
                GROUP BY m.movie_id, m.name, m.introduction, m.date, m.image, m.rating, m.actors
                ORDER BY sum( ep.views ) DESC
                LIMIT $offset , $rowPerPage";
        $query = $this->db->query($sql);
        return $query->result_array();        
    }
    
    /**
     * @param  $offset
     * @param  $rowPerPage
     * @return movie array
     */
    function getMostRateMovies($offset, $rowPerPage){
        $this->db->cache_on();
        $sql = "SELECT m.movie_id, m.name, m.introduction, m.date, m.image, m.rating, sum( ep.views ) as views
                FROM movies m, movie_episode ep
                WHERE ep.movie_id = m.movie_id
                GROUP BY m.movie_id, m.name, m.introduction, m.date, m.image, m.rating, m.actors
                ORDER BY m.rating, m.total_votes DESC
                LIMIT $offset , $rowPerPage";
        $query = $this->db->query($sql);
        return $query->result_array();        
    }

    /**
     * @param  $movieID
     * @return string
     */
    function getMovieImage($movieID){
        $this->db->cache_on();
        $sql = "SELECT image FROM movies WHERE movie_id = $movieID";
        $query = $this->db->query($sql);
        $result = $query->row_array();
        return $result['image'];
    }

    function getAll(){
        $this->db->cache_on();
        $data = array();
        $data['mostViews'] = $this->getMostReviewMovies(0, 10);
        $data['topRate'] = $this->getMostRateMovies(0, 10);
        $data['latest'] = $this->getLatestMovies(0, 10);
        return $data;
    }
    
    /**
     * @param  $name
     * @param  $offset
     * @param  $rowsPerPage
     * @param  $maxPage
     * @return movie array
     */
    function searchByName($name, $offset, $rowsPerPage, &$maxPage){
        $this->db->cache_on();
        //xu ly cac ky tu dac biet
        $search = array("'","\"");
        $replace = array("\'","\"");
        $name = str_replace($search, $replace, $name);
        //count so numrows
        $sql = "SELECT count( * ) AS numrows
                FROM (
                SELECT m.movie_id
                FROM movies m, movie_episode ep
                WHERE ep.movie_id = m.movie_id
                AND m.name LIKE '%$name%'
                GROUP BY m.movie_id
                ) AS temp";

        $query = $this->db->query($sql);
        $temp = $query->row_array();
        $maxPage = ceil($temp['numrows'] / $rowsPerPage);

        //$name = $this->db->escape($name);
        $sql = "SELECT m.movie_id AS movie_id, m.name AS movie_name, m.main_content AS movie_main_content, m.introduction as movie_introduction, m.date as movie_date,
m.image as movie_image, m.rating as movie_rating, m.actors as movie_actors, sum( ep.views ) AS movie_views
                FROM movies m, movie_episode ep
                WHERE ep.movie_id = m.movie_id
                AND m.name LIKE '%$name%'
                GROUP BY m.movie_id, m.name, m.introduction, m.date, m.image, m.rating, m.actors
                 LIMIT $offset , $rowsPerPage";
        //run query
        //escape string to like query
        $query = $this->db->query($sql);
        return $query->result_array();
    }
    
    /**
     * @param  $categoryID
     * @param  $name
     * @param  $offset
     * @param  $rowsPerPage
     * @param  $maxPage
     * @return movie array
     */
    function searchByCategory($categoryID, $name, $offset, $rowsPerPage, &$maxPage){
        $this->db->cache_on();
        //xu ly cac ky tu dac biet
        $search = array(
            "'",
            "\""
        );
        $replace = array(
            "\'",
            "\""
        );
        $name = str_replace($search, $replace, $name);
        $sql = "SELECT count(m.movie_id) AS numrows
                FROM movies m
                WHERE m.name LIKE '%" . $name . "%'
                AND m.movie_category_id = $categoryID";
        $query = $this->db->query($sql);
        $temp = $query->row_array();
        $maxPage = ceil($temp['numrows'] / $rowsPerPage);
        
        $sql = "SELECT m.movie_id AS movie_id, m.name AS movie_name, m.main_content AS movie_main_content, m.introduction as movie_introduction, m.date as movie_date, m.image as movie_image, m.rating as movie_rating, m.actors as movie_actors, sum( ep.views ) AS movie_views
                FROM movies m, movie_episode ep
                WHERE ep.movie_id = m.movie_id
                AND m.name LIKE '%" . $name . "%'
                AND m.movie_category_id = $categoryID
                GROUP BY m.movie_id, m.name, m.introduction, m.date, m.image, m.rating, m.actors
                LIMIT $offset , $rowsPerPage";
        //run query
        $query = $this->db->query($sql);
        return $query->result_array();        
    }
    
    
    /**
     * @param  $categoryID
     * @param  $name
     * @param  $offset
     * @param  $rowsPerPage
     * @param  $maxPage
     * @return movie array
     */
    function searchByActors($categoryID, $name, $offset, $rowsPerPage, &$maxPage){
        $this->db->cache_on();
        //xu ly cac ky tu dac biet
        $search = array(
            "'",
            "\""
        );
        $replace = array(
            "\'",
            "\""
        );
        $name = str_replace($search, $replace, $name);
        if($categoryID != 0 && $categoryID != -1)
            $sql = "SELECT count(m.movie_id) AS numrows
                    FROM movies m
                    WHERE m.actors LIKE '%" . $name . "%'
                    AND m.movie_category_id = $categoryID";
        else
            $sql = "SELECT count(m.movie_id) AS numrows
                    FROM movies m
                    WHERE m.actors LIKE '%" . $name . "%'";
        $query = $this->db->query($sql);
        $temp = $query->row_array();
        $maxPage = ceil($temp['numrows'] / $rowsPerPage);

        if($categoryID != 0 && $categoryID != -1)
            $sql = "SELECT m.movie_id AS movie_id, m.name AS movie_name, m.main_content AS movie_main_content, m.introduction as movie_introduction, m.date as movie_date, m.image as movie_image, m.rating as movie_rating, m.actors as movie_actors, sum( ep.views ) AS movie_views
                FROM movies m, movie_episode ep
                WHERE ep.movie_id = m.movie_id
                AND m.actors LIKE '%" . $name . "%'
                AND m.movie_category_id = $categoryID
                GROUP BY m.movie_id, m.name, m.introduction, m.date, m.image, m.rating, m.actors
                LIMIT $offset , $rowsPerPage";
        else
            $sql = "SELECT m.movie_id AS movie_id, m.name AS movie_name, m.main_content AS movie_main_content, m.introduction as movie_introduction, m.date as movie_date, m.image as movie_image, m.rating as movie_rating, m.actors as movie_actors, sum( ep.views ) AS movie_views
                FROM movies m, movie_episode ep
                WHERE ep.movie_id = m.movie_id
                AND m.actors LIKE '%" . $name . "%'
                GROUP BY m.movie_id, m.name, m.introduction, m.date, m.image, m.rating, m.actors
                LIMIT $offset , $rowsPerPage";
        //run query
        $query = $this->db->query($sql);
        
        return $query->result_array(); 
    }
    
    /**
     * @return int
     */
    function addMovie(){
        $data = array(
            'name' => $this->_name,
            'main_content' => $this->_main_content,
            'introduction' => $this->_introduction,
            'actors' => $this->_actors,
            'image' => $this->_image,
            'movie_category_id' => $this->_movie_category_id,
            'date' => $this->_date,
            'year' => $this->_year
        );
        $this->db->insert($this->_table, $data);
        return $this->db->insert_id();
    }
    
    
    /**
     *luu movie nay vao csdl
     * @param type $movieInfo mang chua cac thanh phan cua movie
     */
    function updateMovie($movieInfo){
        //movie data
        if($movieInfo['image'] == '')
            $data = array(
                'movie_category_id' => $movieInfo['movie_category_id'],
                'name' => $movieInfo['name'],
                'main_content' => $movieInfo['main_content'],
                'introduction' => $movieInfo['introduction'],
                'actors' => $movieInfo['actors'],
                'year' => $movieInfo['year']
            );
        else
            $data = array(
                'movie_category_id' => $movieInfo['movie_category_id'],
                'name' => $movieInfo['name'],
                'introduction' => $movieInfo['introduction'],
                'image' => $movieInfo['image'],
                'main_content' => $movieInfo['main_content'],
                //'date' => $movieInfo['date'],
                'actors' => $movieInfo['actors'],
                'year' => $movieInfo['year']
                //'views' => $movieInfo['views'],
                //'vote' => $movieInfo['vote'],
                //'total_votes' => $movieInfo['totalvotes']
            );
        
        /**
         * the nao ko ho tro thi tim chuoi thay the
         */
        $search = array(
             "<script>",
             "</script>"
        );
        $replace = array(
            '&lt;script&gt;',
            '&lt;/script&gt;'
        );
            
        //thay the chuoi
        $data['introduction'] = str_replace($search, $replace, $data['introduction']);
        
        
        
        $this->db->where('movie_id', $movieInfo['movie_id']);
        $this->db->update($this->_table, $data);
    }

    function getDifferentMovies($movieID){
        $this->db->cache_on();
        $sql = "SELECT m.movie_id AS movie_id, m.name AS movie_name, m.image AS movie_image
                FROM movies m
                WHERE movie_category_id = (SELECT movie_category_id
                                            FROM movies
                                            WHERE movie_id = $movieID )
                ORDER BY m.rating DESC
                LIMIT 0 , 20";
        $query = $this->db->query($sql);
        return $query->result_array();
    }
    /**
     * lay ra diem trung binh va luot cho diem
     * @param  $movieID
     * @return null neu ko co movieID nay, array('rating', 'total_votes') neu ton tai
     */
    function getRating($movieID){
        //kiem tra neu ko co movieID nay trong csdl thi tra ve null
        $sql = "SELECT rating, total_votes
                FROM movies
                WHERE movie_id = $movieID
                LIMIT 0 , 1";
        $query = $this->db->query($sql);

        //neu cau truy van tra ve ket qua la null

        $result = $query->row_array();
        if(count($result) == 0)
            return null;
        else
            return $result;
    }

    /**
     * cho diem movie nay
     * @param  $movieID
     * @param  $ratingArray mang gom diem trung binh va tong luot cho diem
     * @return void
     */
    function setRating($movieID, $ratingArray){
        $data = array(
            'rating' => $ratingArray['rating'],
            'total_votes' => $ratingArray['total_votes']
        );
        $this->db->where('movie_id', $movieID);
        $this->db->update($this->_table, $data);
    }

    function search($name){
        $search = array("'","\"");
        $replace = array("\'","\"");
        $name = str_replace($search, $replace, $name);
        $sql = "SELECT m.movie_id AS movie_id, m.name AS movie_name, m.year AS movie_year
                FROM movies m
                WHERE name like '%$name%'
                LIMIT 0, 50";
        $query = $this->db->query($sql);

        return $query->result_array(); 
    }
}
?>
